<?php

class JsonController extends Zend_Controller_Action
{
    
    public function init()
    {
        $this->getHelper('layout')->setLayout('json');
    }
    
    public function bookmarkAction()
    {
        $bookmark = new HTech_Model_Bookmark();
        $this->view->items = $bookmark->getJsonItems();
    }    

    public function inspectorAction()
    {        
        //$inspector = new HTech_Model_Inspector();
        //$this->view->inspector = $inspector->getItems();
    } 
    
    public function pwdRecoveryAction()
    {
        $data = $this->_request->getParams();
        $credTreatment = sprintf('? and Status = "Active" 
                    and SystemAccessFromDate <= "%s" 
                    and SystemAccessToDate >= "%s"', date("Y-m-d"), date("Y-m-d"));
        $login = new HTech_Login($data['cl'],$data['db'],$data['usr'],$data['email'],'view_users','Username','Email',$credTreatment);
        if (!$login->authenticate()->isValid()) {
            echo $this->_encode(array('err'=> $this->view->translate('Email not registered or account disabled')));
        } else { 
            $profile = $login->getProfile(null,'Password');
            $token = bin2hex(openssl_random_pseudo_bytes(8));
            $expirationDate = date('Y-m-d H:i:s', time()+60*30);
            /*
             * Save temporary password
             */
            $users = new Zend_Db_Table('users');
            $data = array(
                'TmpPassword' => $token,
                'TmpPasswordDatetime' => $expirationDate);
            $where = $users->getAdapter()->quoteInto('UserId = ?', $profile->UserId);
            $users->update($data, $where);
            /*
             * Email temporary password
             */
            $conf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini');
            $settings = array(
                'ssl' => 'ssl',
                'port' => $conf->smtp->port,
                'auth' => $conf->smtp->auth,
                'username' => $conf->smtp->username,
                'password' => $conf->smtp->password);
            $transport = new Zend_Mail_Transport_Smtp($conf->smtp->server, $settings);
            $mail = new Zend_Mail();
            $mail->setFrom($conf->smtp->email, $conf->smtp->name);
            $mail->addTo($profile->Email, $profile->Username);
            $mail->setSubject($this->view->translate('Password recovery request'));
            $mail->setBodyText($this->view->translate('Temporary password') . ': ' . $token);
            $mail->send($transport);
            echo $this->_encode(array('scs'=> $this->view->translate('A temporaty password valid for the next 30 minutes has been sent')));
        }   
        Zend_Auth::getInstance()->clearIdentity();
        exit;
    } 
    
    protected function _encode($data)
    {
        $options = array();
        $json = '[';
        foreach ($data as $oV => $oT) { $oT = addslashes($oT); $options[] = "{oV: '$oV', oT: '$oT'}"; }
        $json .= implode(',', $options);
        $json .= ']';
        return $json;        
    }
}